﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Infrastructure
{
    /// <summary>
    /// 
    /// </summary>
    public class TreeTableGroup : TreeGroupBase<DataRow>
    {
        DataTable _flatDataTable;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="nodeKey"></param>
        /// <param name="superKey"></param>
        /// <param name="sortKey"></param>
        /// <param name="groupFlatDataTable"></param>
        public TreeTableGroup(object groupId, string nodeKey, string superKey, string sortKey, DataTable groupFlatDataTable)
            : base(groupId)
        {
            _flatDataTable = groupFlatDataTable;
            this.NodeKey = nodeKey;
            this.SuperKey = superKey;
            this.SortKey = sortKey;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        protected override object GetUniqueID(DataRow dataRow)
        {
            return dataRow[this.NodeKey];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        protected override object GetSuperID(DataRow dataRow)
        {
            return dataRow[this.SuperKey];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        protected override object GetSortValue(DataRow dataRow)
        {
            return dataRow[this.SortKey];
        }
        /// <summary>
        /// 分组内扁平化原始数据(自关联数据)
        /// </summary>
        public DataTable FlatData
        {
            get
            {
                return _flatDataTable;
            }
        }
        /// <summary>
        /// 默认根数据
        /// </summary>
        protected override DataRow RootData
        {
            get
            {
                DataRow rootDataRow = null;
                foreach (DataRow dataRow in this.FlatData.Rows) //循环提取根节点
                {
                    var sid = this.GetSuperID(dataRow);
                    if (object.Equals(sid, this.GroupID)) //根节点提取规则为父ID等于组ID
                    {
                        rootDataRow = dataRow;
                        break;
                    }
                }
                return rootDataRow;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public override ITreeNode CreateTreeByNodeID(object nodeId)
        {
            DataRow nodeDataRow = null;
            foreach (DataRow dataRow in this.FlatData.Rows) //
            {
                var id = this.GetUniqueID(dataRow);
                if (object.Equals(id, nodeId)) //
                {
                    nodeDataRow = dataRow;
                    break;
                }
            };

            return CreateTree(nodeDataRow, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        protected override IEnumerable<DataRow> GetChildren(DataRow dataRow)
        {
            var sid = this.GetUniqueID(dataRow);
            DataRow[] childrenData = this.FlatData.Select($"{this.SuperKey}='{sid}' AND {this.NodeKey} != '{sid}'");
            return childrenData;
        }
        /// <summary>
        /// 扁平数据映射，默认直接返回，可重写为返回自定义对象
        /// </summary>
        /// <param name="flatDataRow"></param>
        /// <returns></returns>
        protected override object Mapping(DataRow flatDataRow)
        {
            var columns = flatDataRow.Table.Columns;
            var dict = new Dictionary<string, object>();
            foreach (DataColumn column in columns)
            {
                dict.Add(column.ColumnName, flatDataRow[column]);
            }
            return dict;
        }

    }
}
